/*
 * Copyright (C) 2012 VinaVAC. All rights reserved.
 */
package org.vinavac.mob.converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;
import org.vinavac.entity.Staff;
import org.vinavac.mob.mbean.StaffMobileBean;

/**
 *
 * @author Quang <quangtin3@gmail.com>
 */
@FacesConverter(forClass = Staff.class)
public class StaffMobileConverter extends AbstractMobileConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        if (value == null || value.length() == 0) {
            return null;
        }
        StaffMobileBean controller = (StaffMobileBean) facesContext.getApplication().getELResolver().
                getValue(facesContext.getELContext(), null, "staffMobileBean");
        return controller.find(getKey(value));
    }

    @Override
    public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
        if (object == null) {
            return null;
        }
        if (object instanceof Staff) {
            Staff o = (Staff) object;
            return getStringKey(o.getId());
        } else {
            throw new IllegalArgumentException("object " + object + " is of type " + object.getClass().getName() + "; expected type: " + StaffMobileBean.class.getName());
        }
    }
}
